<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是重定位——实现段的重定位的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV04-06-重定位-02-实现段的重定位">
<meta property="og:url" content="https://sumumm.github.io/post/ccdcfc3c.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是重定位——实现段的重定位的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20230929093431743.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20230929095552392.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20240119232309998.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20240119232813611.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20240119073639853.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20240119075018893.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20240119230542564.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20240119224923460.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20240119231520558.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20240119233245643.png">
<meta property="article:published_time" content="2024-01-21T08:04:05.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.039Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="LV04-裸机开发">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20230929093431743.png">


<link rel="canonical" href="https://sumumm.github.io/post/ccdcfc3c.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/ccdcfc3c.html","path":"post/ccdcfc3c.html","title":"LV04-06-重定位-02-实现段的重定位"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV04-06-重定位-02-实现段的重定位 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E9%87%8D%E5%AE%9A%E4%BD%8D%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="nav-text">一、重定位的概念</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E4%BB%80%E4%B9%88%E6%98%AF%E9%87%8D%E5%AE%9A%E4%BD%8D"><span class="nav-text">1. 什么是重定位</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E9%87%8D%E5%AE%9A%E4%BD%8D%EF%BC%9F"><span class="nav-text">2. 为什么要重定位？</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E6%B1%87%E7%BC%96%E9%87%8D%E5%AE%9A%E4%BD%8Ddata%E6%AE%B5"><span class="nav-text">二、汇编重定位data段</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%8F%82%E8%80%83%E8%8A%AF%E7%89%87%E6%89%8B%E5%86%8C%E7%A1%AE%E5%AE%9A%E7%89%87%E5%86%85RAM%E4%BD%8D%E7%BD%AE"><span class="nav-text">1. 参考芯片手册确定片内RAM位置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E9%93%BE%E6%8E%A5%E8%84%9A%E6%9C%AC%E4%BF%AE%E6%94%B9"><span class="nav-text">2. 链接脚本修改  </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E4%BF%AE%E6%94%B9%E6%B1%87%E7%BC%96%E6%96%87%E4%BB%B6%E9%87%8D%E5%AE%9A%E4%BD%8D-data-%E6%AE%B5"><span class="nav-text">3. 修改汇编文件重定位.data 段  </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-%E6%B5%8B%E8%AF%95%E4%B8%80%E4%B8%8B%E5%9C%A8DDR%E5%86%85%E9%83%A8%E7%9A%84data%E6%AE%B5%E9%87%8D%E5%AE%9A%E4%BD%8D"><span class="nav-text">3.1 测试一下在DDR内部的data段重定位</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E9%87%8D%E5%AE%9A%E4%BD%8D%E5%88%B0%E7%89%87%E5%86%85RAM"><span class="nav-text">3.2 重定位到片内RAM</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-%E6%B5%8B%E8%AF%95%E7%BB%93%E6%9E%9C"><span class="nav-text">3.3 测试结果</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81C-%E5%87%BD%E6%95%B0%E9%87%8D%E5%AE%9A%E4%BD%8D-data-%E6%AE%B5%E5%92%8C%E6%B8%85%E9%99%A4-bss-%E6%AE%B5"><span class="nav-text">三、C 函数重定位 data 段和清除 bss 段  </span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E9%80%9A%E8%BF%87%E6%B1%87%E7%BC%96%E4%BC%A0%E9%80%92%E9%93%BE%E6%8E%A5%E8%84%9A%E6%9C%AC%E5%8F%98%E9%87%8F"><span class="nav-text">1. 通过汇编传递链接脚本变量  </span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E4%BF%AE%E6%94%B9%E6%B1%87%E7%BC%96%E6%96%87%E4%BB%B6"><span class="nav-text">1.1 修改汇编文件  </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E5%AE%9E%E7%8E%B0-copy-data-clean-bss-%E5%87%BD%E6%95%B0"><span class="nav-text">1.2 实现 copy_data, clean_bss 函数  </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E4%BF%AE%E6%94%B9-Makefile"><span class="nav-text">1.3 修改 Makefile  </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-%E6%B5%8B%E8%AF%95%E6%95%88%E6%9E%9C"><span class="nav-text">1.4 测试效果</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-C%E5%87%BD%E6%95%B0%E7%9B%B4%E6%8E%A5%E8%B0%83%E5%8F%96%E9%93%BE%E6%8E%A5%E8%84%9A%E6%9C%AC%E5%8F%98%E9%87%8F"><span class="nav-text">2. C函数直接调取链接脚本变量</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E4%BF%AE%E6%94%B9%E6%B1%87%E7%BC%96%E6%96%87%E4%BB%B6%E4%B8%BA%E7%9B%B4%E6%8E%A5%E8%B0%83%E7%94%A8-C-%E5%87%BD%E6%95%B0"><span class="nav-text">2.1 修改汇编文件为直接调用 C 函数  </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E4%BF%AE%E6%94%B9-init-c-%E9%80%9A%E8%BF%87%E5%87%BD%E6%95%B0%E6%9D%A5%E8%8E%B7%E5%8F%96%E5%8F%82%E6%95%B0"><span class="nav-text">2.2 修改 init.c 通过函数来获取参数  </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E6%B5%8B%E8%AF%95%E6%95%88%E6%9E%9C"><span class="nav-text">2.3 测试效果</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%80%BB%E7%BB%93%EF%BC%9A%E5%A6%82%E4%BD%95%E5%9C%A8-C-%E5%87%BD%E6%95%B0%E4%B8%AD%E4%BD%BF%E7%94%A8%E9%93%BE%E6%8E%A5%E8%84%9A%E6%9C%AC%E5%8F%98%E9%87%8F"><span class="nav-text">3. 总结：如何在 C 函数中使用链接脚本变量  </span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81C%E8%AF%AD%E8%A8%80%E9%87%8D%E5%AE%9A%E4%BD%8D%E5%85%A8%E9%83%A8%E4%BB%A3%E7%A0%81"><span class="nav-text">四、C语言重定位全部代码</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E9%87%8D%E5%AE%9A%E4%BD%8D%E6%AD%A5%E9%AA%A4"><span class="nav-text">1. 重定位步骤</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E4%BF%AE%E6%94%B9%E9%93%BE%E6%8E%A5%E8%84%9A%E6%9C%AC"><span class="nav-text">1.1 修改链接脚本  </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E4%BF%AE%E6%94%B9-init-c"><span class="nav-text">1.2 修改 init.c  </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-3-%E4%BF%AE%E6%94%B9%E6%B1%87%E7%BC%96%E6%96%87%E4%BB%B6"><span class="nav-text">1.3 修改汇编文件  </span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-4-%E6%B5%8B%E8%AF%95%E6%95%88%E6%9E%9C-1"><span class="nav-text">1.4 测试效果</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E4%BD%8D%E7%BD%AE%E6%97%A0%E5%85%B3%E7%A0%81"><span class="nav-text">2. 位置无关码</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%A6%82%E4%BD%95%E5%86%99%E4%BD%8D%E7%BD%AE%E6%97%A0%E5%85%B3%E7%A0%81%EF%BC%9F"><span class="nav-text">3. 如何写位置无关码？</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/ccdcfc3c.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV04-06-重定位-02-实现段的重定位 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV04-06-重定位-02-实现段的重定位
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2024-01-21 16:04:05" itemprop="dateCreated datePublished" datetime="2024-01-21T16:04:05+08:00">2024-01-21</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">嵌入式开发</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/" itemprop="url" rel="index"><span itemprop="name">02IMX6ULL平台</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">LV04-裸机开发</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>5.8k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>21 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是重定位——实现段的重定位的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows版本</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu版本</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr>    <tr>        <td align="center">终端软件</td>        <td align="left">MobaXterm(Professional Edition v23.0 Build 5042 (license))</td>      </tr>    <tr>        <td align="center">Linux开发板</td>        <td align="left">正点原子 i.MX6ULL Linux 阿尔法开发板</td>      </tr>    <tr>        <td align="center">uboot</td>        <td align="left">NXP官方提供的uboot，NXP提供的版本为uboot-imx-rel_imx_4.1.15_2.1.0_ga(使用的uboot版本为U-Boot 2016.03)</td>      </tr>    <tr>        <td align="center">linux内核</td>        <td align="left">linux-4.15(NXP官方提供)</td>      </tr>    <tr>        <td align="center">Win32DiskImager</td>        <td align="left">Win32DiskImager v1.0</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="5">官方网站</td>        <td align="left"><a href="https://www.arm.com/" target="_blank">https://www.arm.com/</a></td>        <td align="left">ARM官方网站，在这里我们可以找到Cotex-Mx以及ARMVx的一些文档</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxp.com.cn/" target="_blank">https://www.nxp.com.cn/ </a></td>        <td align="left">NXP官方网站</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxpic.org.cn/" target="_blank">https://www.nxpic.org.cn/</a></td><td align="left">NXP 官方社区</td>    </tr>    <tr>        <td align="left"><a href="https://u-boot.readthedocs.io/en/latest/" target="_blank">https://u-boot.readthedocs.io/en/latest/</a></td><td align="left">u-boot官网</td>    </tr>    <tr>        <td align="left"><a href="https://www.kernel.org/" target="_blank">https://www.kernel.org/</a></td><td align="left">linux内核官网</td>    </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">分类</td>        <td align="center">网址</td>        <td align="center">说明</td>    </tr>    <tr>        <td align="center" rowspan="4">NXP</td>        <td align="left"><a href="https://github.com/nxp-imx" target="_blank">https://github.com/nxp-imx</a></td>        <td align="left">NXP imx开发资源GitHub组织，里边会有u-boot和linux内核的仓库</td>    </tr>    <tr>        <td align="left"><a href="https://elixir.bootlin.com/linux/latest/source" target="_blank">https://elixir.bootlin.com/linux/latest/source</a></td>        <td align="left">在线阅读linux kernel源码</td>    </tr>    <tr>        <td align="left"><a href="https://github.com/nxp-imx/linux-imx/releases/tag/rel_imx_4.1.15_2.1.0_ga" target="_blank">nxp-imx/linux-imx/releases/tag/rel_imx_4.1.15_2.1.0_ga</a></td>        <td align="left">NXP linux内核仓库tags中的rel_imx_4.1.15_2.1.0_ga</td>    </tr>    <tr>        <td align="left"><a href="https://github.com/nxp-imx/uboot-imx/releases/tag/rel_imx_4.1.15_2.1.0_ga" target="_blank">nxp-imx/uboot-imx/releases/tag/rel_imx_4.1.15_2.1.0_ga</a></td>        <td align="left">NXP u-boot仓库tags中的rel_imx_4.1.15_2.1.0_ga</td>    </tr>    <tr>        <td align="center" rowspan="2">I.MX6ULL</td>        <td align="left"><a href="https://www.nxp.com.cn/docs/en/data-sheet/IMX6ULLIEC.pdf" target="_blank">i.MX 6ULL Applications Processors for Industrial Products</a></td>        <td align="left">I.MX6ULL 芯片手册（datasheet，可以在线查看）</td>    </tr>    <tr>        <td align="left"><a href="https://www.nxp.com.cn/webapp/Download?colCode=IMX6ULLRM&lang_cd=zh" target="_blank">i.MX 6ULL Applications ProcessorReference Manual</a></td>        <td align="left">I.MX6ULL 参考手册（下载后才能查看，需要登录NXP官网）</td>    </tr>        <tr>        <td align="center" rowspan="5">ARM</td>        <td align="left"><a href="https://developer.arm.com/documentation/ddi0464/latest" target="_blank">Cortex-A7 MPCore Technical Reference Manual</a></td>        <td align="left">Cortex-A7 MPCore技术参考手册</td>    </tr>    <tr>        <td align="left"><a href="https://developer.arm.com/documentation/ddi0406/cd/?lang=en" target="_blank">ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition</a></td>        <td align="left">ARM架构参考手册ARMv7-A和ARMv7-R版</td>    </tr>    <tr>        <td align="left"><a href="https://developer.arm.com/documentation/ihi0069/h/?lang=en" target="_blank">Arm Generic Interrupt Controller Architecture Specification- version 3 and version 4</a></td>        <td align="left">Arm通用中断控制器架构规范-版本3和版本4</td>    </tr>    <tr>        <td align="left"><a href="https://developer.arm.com/documentation/ihi0048/bb/?lang=en" target="_blank">ARM Generic Interrupt Controller Architecture Specification - Version 2.0</a></td>        <td align="left">Arm通用中断控制器架构规范-版本2.0</td>    </tr>    <tr>        <td align="left"><a href="https://developer.arm.com/documentation/den0013/d/?lang=en" target="_blank">ARM Cortex-A Series Programmer's Guide for ARMv7-A</a></td>        <td align="left">Cortex-A系列ARMv7-A编程指南</td>    </tr></table>
              </div>
            </details>

<h1 id="一、重定位的概念"><a href="#一、重定位的概念" class="headerlink" title="一、重定位的概念"></a><font size=3>一、重定位的概念</font></h1><h2 id="1-什么是重定位"><a href="#1-什么是重定位" class="headerlink" title="1. 什么是重定位"></a><font size=3>1. 什么是重定位</font></h2><p>接触过 S3C2440 的话应该会了解到，在程序运行之前我们需要手动将.bin文件上的全部代码从 Nor Flash 或 Nand Flash 拷贝到 SDRAM 上。对于 imx6ull来说，这部分拷贝代码的操作由 Boot Rom 自动完成，板子上电后 boot Rom 会将映像文件从启动设备(TF 卡、 eMMC)自动拷贝到 DDR3 内存上。上述拷贝代码的过程就是<strong>重定位</strong>。  </p>
<p>那么 boot Rom 应该将映像文件拷贝到内存的哪个位置呢？这部分内容已经《LV04-01-IMX6ULL启动流程-03-映像文件》中学习过了 。映像文件包含多个部分，其中.bin 文件的起始地址由地址entry 决定，需要在 Makefile 中手动配置。 </p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./mkimage -n imximage.cfg.cfgtmp -T imximage -e 0x80100000 -d demo.bin demo.imx</span><br></pre></td></tr></table></figure>

<p>按照上述的配置，整个映像文件被自动重定位到 DDR 内存上，其中.bin 文件的起始地址为 0x80100000。重定位结束后， CPU 会从这个地址读取第一条指令开始执行程序。  </p>
<h2 id="2-为什么要重定位？"><a href="#2-为什么要重定位？" class="headerlink" title="2. 为什么要重定位？"></a><font size=3>2. 为什么要重定位？</font></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">const</span> <span class="type">char</span> * argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	Uart_Init();	<span class="comment">//初始化uart串口</span></span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;\n\r&quot;</span>);</span><br><span class="line">	<span class="comment">/* 在串口上输出g_charA */</span></span><br><span class="line">	<span class="keyword">while</span> (<span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		PutChar(g_charA);</span><br><span class="line">		g_charA++;</span><br><span class="line">		delay(<span class="number">1000000</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上述代码在程序运行时， CPU 需要不断地访问 DDR3 内存来获取 g_charA 的值，访问DDR3 会花费大量的时间，那么如何提升访问的效率呢？</p>
<p>答：在程序运行先前将 data 段的数据重定位到 imx6ull 的片内 RAM 上，因为CPU 访问片内 RAM 的速度远快于访问 DDR3 的速度。  </p>
<h1 id="二、汇编重定位data段"><a href="#二、汇编重定位data段" class="headerlink" title="二、汇编重定位data段"></a><font size=3>二、汇编重定位data段</font></h1><p>完整代码可以看这里：</p>
<h2 id="1-参考芯片手册确定片内RAM位置"><a href="#1-参考芯片手册确定片内RAM位置" class="headerlink" title="1. 参考芯片手册确定片内RAM位置"></a><font size=3>1. 参考芯片手册确定片内RAM位置</font></h2><p>我们可以查看参考手册的《Chapter 2: Memory Maps》  ：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20230929093431743.png" alt="image-20230929093431743" style="zoom:50%;" />

<p>参考芯片手册得到片内 RAM 的地址为： 0x900000 ~ 0x91FFFF。所以我们将.data 段重定位后的地址设置为 0x900000。  </p>
<h2 id="2-链接脚本修改"><a href="#2-链接脚本修改" class="headerlink" title="2. 链接脚本修改  "></a><font size=3>2. 链接脚本修改  </font></h2><p>创建一个变量用来存储.data 段的起始加载地址。  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">   . = ALIGN(<span class="number">4</span>);</span><br><span class="line">   .rodata : &#123; *(.rodata) &#125;</span><br><span class="line">   . = ALIGN(<span class="number">4</span>);</span><br><span class="line">data_load_addr = .;<span class="comment">//将当前地址存储在变量中（大概的值为 0x8880xxxx）</span></span><br></pre></td></tr></table></figure>

<p>将.data 段的运行地址(runtime address)设定为 0x900000。加载地址由变量 data_load_addr 确定。这样设置后，在.bin 文件中“ .data”段仍旧存储在 “ .rodata ” 段之后。但在程序运行时， CPU 会从 0x900000 开始的空间内读取 “.data 段” 的值。  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.data <span class="number">0x900000</span> : AT(data_load_addr)</span><br></pre></td></tr></table></figure>

<p>下面我们将重定位后.data 段的起始地址存储在变量 data_start，重定位后的.data 段的结束地址存储在变量 data_end，这两个变量将供汇编文件调用。  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  data_start = . ;</span><br><span class="line">  *(.data) </span><br><span class="line">  data_end = . ;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>修改后的链接脚本如下所示:</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">SECTIONS &#123;</span><br><span class="line">    . = <span class="number">0x80100000</span>;</span><br><span class="line"></span><br><span class="line">    . = ALIGN(<span class="number">4</span>);</span><br><span class="line">    .text      :</span><br><span class="line">    &#123;</span><br><span class="line">      *(.text)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    . = ALIGN(<span class="number">4</span>);</span><br><span class="line">    .rodata : &#123; *(.rodata) &#125;</span><br><span class="line"></span><br><span class="line">    . = ALIGN(<span class="number">4</span>);</span><br><span class="line"></span><br><span class="line">    data_load_addr = .;</span><br><span class="line">    .data <span class="number">0x900000</span> : AT(data_load_addr)</span><br><span class="line">    &#123;</span><br><span class="line">      data_start = . ;</span><br><span class="line">      *(.data) </span><br><span class="line">      data_end = . ;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    . = ALIGN(<span class="number">4</span>);</span><br><span class="line">    __bss_start = .;</span><br><span class="line">    .bss : &#123; *(.bss) *(.COMMON) &#125;</span><br><span class="line">    __bss_end = .;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>通过上述操作， CPU 虽然会去片内 RAM 中读取.data 段数据，但<strong>实际上片内 RAM并没有准备好.data 段的数据</strong>，如下图所示。下面我们将通过汇编将 DDR 内存上的.data 段数据重定位到片内 RAM 上。  </p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20230929095552392.png" alt="image-20230929095552392" style="zoom:50%;" />

<h2 id="3-修改汇编文件重定位-data-段"><a href="#3-修改汇编文件重定位-data-段" class="headerlink" title="3. 修改汇编文件重定位.data 段  "></a><font size=3>3. 修改汇编文件重定位.data 段  </font></h2><h3 id="3-1-测试一下在DDR内部的data段重定位"><a href="#3-1-测试一下在DDR内部的data段重定位" class="headerlink" title="3.1 测试一下在DDR内部的data段重定位"></a><font size=3>3.1 测试一下在DDR内部的data段重定位</font></h3><p>这里完整的测试代码可以看这里：<a target="_blank" rel="noopener" href="https://gitee.com/sumumm/imx6ull-bare-demo/tree/master/09_RELOCATION/03_without_relocation">09_RELOCATION&#x2F;03_without_relocation · sumumm&#x2F;imx6ull-bare-demo - 码云 - 开源中国 (gitee.com)</a>。我们可以将之前定义的各个变量的地址打印出来：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;uart.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;my_printf.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">char</span> g_charA = <span class="string">&#x27;A&#x27;</span>;			<span class="comment">//存储在 .data段</span></span><br><span class="line"><span class="type">const</span> <span class="type">char</span> g_charB = <span class="string">&#x27;B&#x27;</span>;	<span class="comment">//存储在 .rodata段</span></span><br><span class="line"><span class="type">const</span> <span class="type">char</span> g_charC;			<span class="comment">//存储在 .bss段</span></span><br><span class="line"><span class="type">int</span> g_intA = <span class="number">0</span>;				<span class="comment">//存储在 .bss段</span></span><br><span class="line"><span class="type">int</span> g_intB;					<span class="comment">//存储在 .bss段</span></span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">delay</span> <span class="params">(<span class="keyword">volatile</span> <span class="type">int</span> time)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">while</span>(time--);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">(<span class="type">int</span> argc, <span class="type">const</span> <span class="type">char</span> * argv[])</span></span><br><span class="line">&#123;</span><br><span class="line">	Uart_Init();	<span class="comment">//初始化uart串口</span></span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;g_charA=%d &amp;g_charA=0x%x\n\r&quot;</span>, g_charA, &amp;g_charA);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;g_charB=%d &amp;g_charB=0x%x\n\r&quot;</span>, g_charB, &amp;g_charB);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;g_charC=%d &amp;g_intA=0x%x\n\r&quot;</span>, g_charC, &amp;g_charC);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;g_intA=%d &amp;g_intA=0x%x\n\r&quot;</span>, g_intA, &amp;g_intA);</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;g_intB=%d &amp;g_intB=0x%x\n\r&quot;</span>, g_intB, &amp;g_intB);</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* 在串口上输出g_charA */</span></span><br><span class="line">	<span class="keyword">while</span> (<span class="number">1</span>)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="comment">//printf(&quot;g_intA=%d &amp;g_intA=0x%x\n\r&quot;, g_intA, &amp;g_intA);</span></span><br><span class="line">		g_charA++;</span><br><span class="line">		delay(<span class="number">1000000</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>我们的链接文件如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">SECTIONS &#123;</span><br><span class="line">    . = <span class="number">0x80100000</span>;</span><br><span class="line"></span><br><span class="line">    . = ALIGN(<span class="number">4</span>);</span><br><span class="line">    .text      :</span><br><span class="line">    &#123;</span><br><span class="line">      *(.text)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    . = ALIGN(<span class="number">4</span>);</span><br><span class="line">    .rodata : &#123; *(.rodata) &#125;</span><br><span class="line"></span><br><span class="line">    . = ALIGN(<span class="number">4</span>);</span><br><span class="line">    .data <span class="number">0x80200000</span> : &#123; *(.data) &#125;</span><br><span class="line"></span><br><span class="line">    . = ALIGN(<span class="number">4</span>);</span><br><span class="line">    __bss_start = .;</span><br><span class="line">    .bss : &#123; *(.bss) *(.COMMON) &#125;</span><br><span class="line">    __bss_end = .;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>可以看到打印信息如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20240119232309998.png" alt="image-20240119232309998" style="zoom:50%;" />

<p>然后我们修改链接文件如下：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">SECTIONS &#123;</span><br><span class="line">    . = <span class="number">0x80100000</span>;</span><br><span class="line"></span><br><span class="line">    . = ALIGN(<span class="number">4</span>);</span><br><span class="line">    .text :</span><br><span class="line">    &#123;</span><br><span class="line">      *(.text)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    . = ALIGN(<span class="number">4</span>);</span><br><span class="line">    .rodata : &#123; *(.rodata) &#125;</span><br><span class="line"></span><br><span class="line">    . = ALIGN(<span class="number">4</span>);</span><br><span class="line">    .data <span class="number">0x80140000</span> : &#123; *(.data) &#125;</span><br><span class="line"></span><br><span class="line">    . = ALIGN(<span class="number">4</span>);</span><br><span class="line">    __bss_start = .;</span><br><span class="line">    .bss : &#123; *(.bss) *(.COMMON) &#125;</span><br><span class="line">    __bss_end = .;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>会看到变量的地址也随着发生了变化：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20240119232813611.png" alt="image-20240119232813611" style="zoom:50%;" />

<h3 id="3-2-重定位到片内RAM"><a href="#3-2-重定位到片内RAM" class="headerlink" title="3.2 重定位到片内RAM"></a><font size=3>3.2 重定位到片内RAM</font></h3><p>完整代码可以看这里：<a target="_blank" rel="noopener" href="https://gitee.com/sumumm/imx6ull-bare-demo/tree/master/09_RELOCATION/04_manual_relocate_data">09_RELOCATION&#x2F;04_manual_relocate_data · sumumm&#x2F;imx6ull-bare-demo - 码云 - 开源中国 (gitee.com)</a>。设置完栈后直接跳转到 copy_data 函数重定位 data 段。  </p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">.text</span><br><span class="line">.global  _start</span><br><span class="line"></span><br><span class="line">_start: 				</span><br><span class="line"></span><br><span class="line">	/* 设置栈 */</span><br><span class="line">	ldr  sp,=0x80200000</span><br><span class="line"></span><br><span class="line">	/* 重定位data段 */</span><br><span class="line">	bl copy_data</span><br><span class="line"></span><br><span class="line">	/* 清除bss段 */</span><br><span class="line">	bl clean_bss</span><br></pre></td></tr></table></figure>

<p>燃弧我们来实现这个 copy_data 函数  ：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">copy_data:</span><br><span class="line">	/* 重定位data段 */</span><br><span class="line">	ldr r1, =data_load_addr  /* data段的加载地址 (0x8010....) */</span><br><span class="line">	ldr r2, =data_start 	 /* data段重定位地址, 0x900000 */</span><br><span class="line">	ldr r3, =data_end 	 /* data段结束地址(重定位后地址 0x90....) */</span><br><span class="line">cpy:</span><br><span class="line">	ldr r4, [r1] 			 /* 从r1读到r4 */</span><br><span class="line">	str r4, [r2] 			 /* r4存放到r2 */</span><br><span class="line">	add r1, r1, #4 			 /* r1+1 */</span><br><span class="line">	add r2, r2, #4			 /* r2+1 */</span><br><span class="line">	cmp r2, r3 		         /* r2 r3比较 */</span><br><span class="line">	bne cpy 		         /* 如果不等则继续拷贝 */</span><br><span class="line"></span><br><span class="line">	mov pc, lr</span><br></pre></td></tr></table></figure>

<h3 id="3-3-测试结果"><a href="#3-3-测试结果" class="headerlink" title="3.3 测试结果"></a><font size=3>3.3 测试结果</font></h3><p>我们烧写到开发板，会有以下打印信息：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20240119073639853.png" alt="image-20240119073639853" style="zoom:50%;" />

<p>可以看到.data已经被定位到0x00900000。</p>
<h1 id="三、C-函数重定位-data-段和清除-bss-段"><a href="#三、C-函数重定位-data-段和清除-bss-段" class="headerlink" title="三、C 函数重定位 data 段和清除 bss 段  "></a><font size=3>三、C 函数重定位 data 段和清除 bss 段  </font></h1><p>目前为止我们已经通过汇编实现了重定位 data 段和清除 bss 段。为了让汇编程序更加简洁，这一节中我们将通过 C 语言实现重定位 data 段和清除 bss段。  </p>
<h2 id="1-通过汇编传递链接脚本变量"><a href="#1-通过汇编传递链接脚本变量" class="headerlink" title="1. 通过汇编传递链接脚本变量  "></a><font size=3>1. 通过汇编传递链接脚本变量  </font></h2><p>这一部分我们来学习通过汇编文件获得链接脚本中的变量，再将这些变量传递给 C 函数。  完整代码可以看这里：<a target="_blank" rel="noopener" href="https://gitee.com/sumumm/imx6ull-bare-demo/tree/master/09_RELOCATION/05_relocate_data_with_c_use_start">09_RELOCATION&#x2F;05_relocate_data_with_c_use_start · sumumm&#x2F;imx6ull-bare-demo - 码云 - 开源中国 (gitee.com)</a></p>
<h3 id="1-1-修改汇编文件"><a href="#1-1-修改汇编文件" class="headerlink" title="1.1 修改汇编文件  "></a><font size=3>1.1 修改汇编文件  </font></h3><p>打开 start.S 将之前的汇编函数 copy_data, clean_bss 删除，改为直接调用C 函数。在调用对应的 C 函数之前，需要通过寄存器 r0~r4 将 C 函数的参数准备好。  </p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">.text</span><br><span class="line">.global  _start</span><br><span class="line"></span><br><span class="line">_start: 				</span><br><span class="line"></span><br><span class="line">	/* 设置栈 */</span><br><span class="line">	ldr  sp,=0x80200000</span><br><span class="line"></span><br><span class="line">	/* 重定位data段 */</span><br><span class="line">	ldr r0, =data_load_addr  /* data段的加载地址 (0x8010....) */</span><br><span class="line">	ldr r1, =data_start 	 /* data段重定位地址, 0x900000 */</span><br><span class="line">	ldr r2, =data_end 	 /* data段结束地址(重定位后地址 0x90....) */</span><br><span class="line">	sub r2, r2, r1	         /* r2的值为data段的长度 */	</span><br><span class="line"></span><br><span class="line">	bl copy_data	         /* 跳转到函数copy_data并将r0,r1,r2作为函数参数传入 */</span><br><span class="line"></span><br><span class="line">	/* 清除bss段 */</span><br><span class="line">	ldr r0, =__bss_start	</span><br><span class="line">	ldr r1, =__bss_end</span><br><span class="line"></span><br><span class="line">	bl clean_bss		/* 跳转到函数clean_bss并将r0, r1作为函数参数传入*/</span><br><span class="line"></span><br><span class="line">	/* 跳转到主函数 */</span><br><span class="line">	bl main</span><br><span class="line"></span><br><span class="line">halt:</span><br><span class="line">	b  halt </span><br></pre></td></tr></table></figure>

<h3 id="1-2-实现-copy-data-clean-bss-函数"><a href="#1-2-实现-copy-data-clean-bss-函数" class="headerlink" title="1.2 实现 copy_data, clean_bss 函数  "></a><font size=3>1.2 实现 copy_data, clean_bss 函数  </font></h3><p>我们创建程序文件 init.c ，在里面实现 copy_data, clean_bss 函数 ：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">copy_data</span> <span class="params">(<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *src, <span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *dest, <span class="type">unsigned</span> <span class="type">int</span> len)</span>  <span class="comment">/* src, dest, len */</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">unsigned</span> <span class="type">int</span> i = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">while</span> (i &lt; len)</span><br><span class="line">	&#123;</span><br><span class="line">		*dest++ = *src++;</span><br><span class="line">		i += <span class="number">4</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">clean_bss</span> <span class="params">(<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *start, <span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *end)</span>  <span class="comment">/* start, end */</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">while</span> (start &lt;= end)</span><br><span class="line">	&#123;</span><br><span class="line">		*start++ = <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<ul>
<li><p>对于 copy_data 函数来说，参数 src, dest, len 分别对应汇编文件中r0, r1, r2 的值。</p>
</li>
<li><p>对于 clean_bss 函数来说，参数 start, end 分别对应汇编文件中 r0,r1 的值</p>
</li>
</ul>
<h3 id="1-3-修改-Makefile"><a href="#1-3-修改-Makefile" class="headerlink" title="1.3 修改 Makefile  "></a><font size=3>1.3 修改 Makefile  </font></h3><p>修改 Makefile 文件，编译 init.c 并链接 init.o。  </p>
<h3 id="1-4-测试效果"><a href="#1-4-测试效果" class="headerlink" title="1.4 测试效果"></a><font size=3>1.4 测试效果</font></h3><p>我们编译并烧写到开发板中，看到以下打印信息说明我们在C语言实现了重定位和清除BSS段：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20240119075018893.png" alt="image-20240119075018893" style="zoom:50%;" />

<h2 id="2-C函数直接调取链接脚本变量"><a href="#2-C函数直接调取链接脚本变量" class="headerlink" title="2. C函数直接调取链接脚本变量"></a><font size=3>2. C函数直接调取链接脚本变量</font></h2><p>上一节中 C 函数需要通过汇编文件传入参数，在这一节我们将进一步改进 C函数，使得 C 函数跳过汇编文件，直接从链接脚本中调用所需变量。完整代码可以看这里：<a target="_blank" rel="noopener" href="https://gitee.com/sumumm/imx6ull-bare-demo/tree/master/09_RELOCATION/06_relocate_data_with_c_use_lds">09_RELOCATION&#x2F;06_relocate_data_with_c_use_lds · sumumm&#x2F;imx6ull-bare-demo - 码云 - 开源中国 (gitee.com)</a></p>
<h3 id="2-1-修改汇编文件为直接调用-C-函数"><a href="#2-1-修改汇编文件为直接调用-C-函数" class="headerlink" title="2.1 修改汇编文件为直接调用 C 函数  "></a><font size=3>2.1 修改汇编文件为直接调用 C 函数  </font></h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">.text</span><br><span class="line">.global  _start</span><br><span class="line"></span><br><span class="line">_start: 				</span><br><span class="line"></span><br><span class="line">	/* 设置栈 */</span><br><span class="line">	ldr  sp,=0x80200000</span><br><span class="line"></span><br><span class="line">	/* 重定位data段 */</span><br><span class="line">	bl copy_data</span><br><span class="line"></span><br><span class="line">	/* 清除bss段 */</span><br><span class="line">	bl clean_bss</span><br><span class="line"></span><br><span class="line">	/* 跳转到主函数 */</span><br><span class="line">	bl main</span><br><span class="line"></span><br><span class="line">halt:</span><br><span class="line">	b  halt </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="2-2-修改-init-c-通过函数来获取参数"><a href="#2-2-修改-init-c-通过函数来获取参数" class="headerlink" title="2.2 修改 init.c 通过函数来获取参数  "></a><font size=3>2.2 修改 init.c 通过函数来获取参数  </font></h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">copy_data</span> <span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 从链接脚本中获得参数 data_load_addr, data_start, data_end */</span></span><br><span class="line">	<span class="keyword">extern</span> <span class="type">int</span> data_load_addr, data_start, data_end;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *dest = (<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *)&amp;data_start;</span><br><span class="line">	<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *end = (<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *)&amp;data_end;</span><br><span class="line">	<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *src = (<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *)&amp;data_load_addr;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* 重定位数据 */</span></span><br><span class="line">	<span class="keyword">while</span> (dest &lt; end)</span><br><span class="line">	&#123;</span><br><span class="line">		*dest++ = *src++;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">clean_bss</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 从lds文件中获得 __bss_start, __bss_end */</span></span><br><span class="line">	<span class="keyword">extern</span> <span class="type">int</span> __bss_end, __bss_start;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *start = (<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *)&amp;__bss_start;</span><br><span class="line">	<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *end = (<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *)&amp;__bss_end;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">while</span> (start &lt;= end)</span><br><span class="line">	&#123;</span><br><span class="line">		*start++ = <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="2-3-测试效果"><a href="#2-3-测试效果" class="headerlink" title="2.3 测试效果"></a><font size=3>2.3 测试效果</font></h3><p>我们编译并烧写到开发板中可以看到如下打印信息，说明我们重定位成功：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20240119230542564.png" alt="image-20240119230542564" style="zoom:50%;" />

<h2 id="3-总结：如何在-C-函数中使用链接脚本变量"><a href="#3-总结：如何在-C-函数中使用链接脚本变量" class="headerlink" title="3. 总结：如何在 C 函数中使用链接脚本变量  "></a><font size=3>3. 总结：如何在 C 函数中使用链接脚本变量  </font></h2><p>我们来总结一下如何在 C 函数中使用链接脚本中定义的变量：</p>
<ul>
<li>（1）在 C 函数中声明该变量为外部变量，用 extern 修饰，例如： extern int _start;</li>
<li>（2）使用取址符号(&amp;)得到该变量的值，例如： int *p &#x3D; &amp;_start;&#x2F;&#x2F;p 的值为 lds 文件中_start 的值。</li>
</ul>
<p>为什么在汇编文件中可以直接使用链接脚本中的变量，而在 C 函数中需要加上取址符号呢？</p>
<p>原因： C 函数中定义一个全局变量 int g_i &#x3D; 10;，程序中必然有 4 字节的空间留出来给这个变量 g_i，然而链接脚本中的变量并不像全局变量一样都保存在.bin 文件中。如果我们在 C 程序中只用到链接脚本变量 a1, a2, a3，那么程序中并不保存这 3 个变量。  但是这些变量的符号，都会保存在 symbol_table 符号表中，如下图所示  </p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20240119224923460.png" alt="image-20240119224923460" style="zoom:50%;" />

<p>从上图中我们注意到：</p>
<ul>
<li>对于全局变量， symbol table 里面存储的是变量的地址；可以通过&amp;g_i得到变量的地址 addr。</li>
<li>对于链接脚本变量， symbol table 里面存储的是变量的值；为了取出这个值， C 代码要通过&amp;a1。</li>
</ul>
<h1 id="四、C语言重定位全部代码"><a href="#四、C语言重定位全部代码" class="headerlink" title="四、C语言重定位全部代码"></a><font size=3>四、C语言重定位全部代码</font></h1><p>对于 imx6ull，它的 boot ROM 功能强大，会帮我们把程序重定位到 DDR3内存上。但对于一些采用其他芯片的板子，这一部分的操作可能需要我们手动去完成。例如 S3C2440 上电后，因为硬件的限制， .bin 文件的前 4k 程序需要将整个程序重定位到大小能够执行整个程序的 SDRAM 上。</p>
<p>为了学习代码重定位所需知识，在这一节中我们将重定位整个.bin 文件到片内 RAM 上。需要注意，虽然将全部代码重定位到片内 RAM 上可以加快命令的执行、数据的读取写入，但是这样的做法并不适合体积较大的程序，因为片内 RAM 只有128KB 空间。    </p>
<h2 id="1-重定位步骤"><a href="#1-重定位步骤" class="headerlink" title="1. 重定位步骤"></a><font size=3>1. 重定位步骤</font></h2><p>完整代码可以看这里：<a target="_blank" rel="noopener" href="https://gitee.com/sumumm/imx6ull-bare-demo/tree/master/09_RELOCATION/07_relocate_all_with_c">09_RELOCATION&#x2F;07_relocate_all_with_c · sumumm&#x2F;imx6ull-bare-demo - 码云 - 开源中国 (gitee.com)</a></p>
<h3 id="1-1-修改链接脚本"><a href="#1-1-修改链接脚本" class="headerlink" title="1.1 修改链接脚本  "></a><font size=3>1.1 修改链接脚本  </font></h3><p>① 修改链接地址为 0x900000</p>
<p>② 删除与.data 段相关的链接脚本变量。</p>
<p>③ 添加变量_load_addr 并将它的值设置为 Makefile 中 entry 地址的值，供 C 函数调用。  </p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">SECTIONS &#123;</span><br><span class="line">    _load_addr = 0x80100000;</span><br><span class="line">    </span><br><span class="line">    . = 0x900000;</span><br><span class="line"></span><br><span class="line">    . = ALIGN(4);</span><br><span class="line">    .text :</span><br><span class="line">    &#123;</span><br><span class="line">      *(.text)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    . = ALIGN(4);</span><br><span class="line">    .rodata : &#123; *(.rodata) &#125;</span><br><span class="line"></span><br><span class="line">    . = ALIGN(4);</span><br><span class="line">    .data : &#123; *(.data) &#125;</span><br><span class="line"></span><br><span class="line">    . = ALIGN(4);</span><br><span class="line">    __bss_start = .;</span><br><span class="line">    .bss : &#123; *(.bss) *(.COMMON) &#125;</span><br><span class="line">    __bss_end = .;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="1-2-修改-init-c"><a href="#1-2-修改-init-c" class="headerlink" title="1.2 修改 init.c  "></a><font size=3>1.2 修改 init.c  </font></h3><p>重定位全部代码和重定位.data 段原理相同。在这里只需要修改 copy_data函数中调用的外部变量。  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">copy_data</span> <span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 从链接脚本中获得参数 _start, __bss_start, */</span></span><br><span class="line">	<span class="keyword">extern</span> <span class="type">int</span> _load_addr, _start, __bss_start;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *dest = (<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *)&amp;_start;			<span class="comment">//_start = 0x900000</span></span><br><span class="line">	<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *end = (<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *)&amp;__bss_start;		<span class="comment">//__bss_start = 0x9xxxxx</span></span><br><span class="line">	<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *src = (<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *)&amp;_load_addr;		<span class="comment">//_load_addr = 0x80100000</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">/* 重定位数据 */</span></span><br><span class="line">	<span class="keyword">while</span> (dest &lt; end)</span><br><span class="line">	&#123;</span><br><span class="line">		*dest++ = *src++;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> <span class="title function_">clean_bss</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="comment">/* 从lds文件中获得 __bss_start, __bss_end */</span></span><br><span class="line">	<span class="keyword">extern</span> <span class="type">int</span> __bss_end, __bss_start;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *start = (<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *)&amp;__bss_start;</span><br><span class="line">	<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *end = (<span class="keyword">volatile</span> <span class="type">unsigned</span> <span class="type">int</span> *)&amp;__bss_end;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">while</span> (start &lt;= end)</span><br><span class="line">	&#123;</span><br><span class="line">		*start++ = <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="1-3-修改汇编文件"><a href="#1-3-修改汇编文件" class="headerlink" title="1.3 修改汇编文件  "></a><font size=3>1.3 修改汇编文件  </font></h3><p>重定位之后，需要使用绝对跳转命令 ldr pc, &#x3D; xxx，跳转到重定位后的地址。  </p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">.text</span><br><span class="line">.global  _start</span><br><span class="line"></span><br><span class="line">_start: 				</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* 设置栈 */</span></span><br><span class="line">	ldr  sp,=<span class="number">0x80200000</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">/* 重定位text, rodata, data段 */</span></span><br><span class="line">	bl copy_data</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* 清除bss段 */</span></span><br><span class="line">	bl clean_bss</span><br><span class="line"></span><br><span class="line">	<span class="comment">/* 跳转到主函数 */</span></span><br><span class="line">	<span class="comment">// bl main	/* 相对跳转，程序仍在DDR3内存中执行 */</span></span><br><span class="line">	ldr pc, =main 	<span class="comment">/* 绝对跳转，程序在片内RAM中执行 */</span></span><br><span class="line"></span><br><span class="line">halt:</span><br><span class="line">	b  halt </span><br><span class="line">	</span><br></pre></td></tr></table></figure>

<h3 id="1-4-测试效果-1"><a href="#1-4-测试效果-1" class="headerlink" title="1.4 测试效果"></a><font size=3>1.4 测试效果</font></h3><p>我们能看到以下打印信息说明重定位成功，可以看到所有的变量都被重定义到片内RAM了。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20240119231520558.png" alt="image-20240119231520558" style="zoom:50%;" />

<h2 id="2-位置无关码"><a href="#2-位置无关码" class="headerlink" title="2. 位置无关码"></a><font size=3>2. 位置无关码</font></h2><p>查看上述程序的反汇编发现，在重定位函数 copy_data 执行之前，已经涉及到了片内 RAM 上的地址，但此时片内 RAM 上并没有任何程序，那为什么程序还能正常运行呢？</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/01%E5%B5%8C%E5%85%A5%E5%BC%8F%E5%BC%80%E5%8F%91/02IMX6ULL%E5%B9%B3%E5%8F%B0/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/LV04-06-%E9%87%8D%E5%AE%9A%E4%BD%8D-02-%E5%AE%9E%E7%8E%B0%E6%AE%B5%E7%9A%84%E9%87%8D%E5%AE%9A%E4%BD%8D/img/image-20240119233245643.png" alt="image-20240119233245643" style="zoom: 50%;" />

<p>dis 文件中左边的 90000xx 是链接地址，表示程序运行“应该位于这里”。但是实际上，我们一上电， boot ROM 把程序放到 0x80100000 去了。所以一开始运行这些指令时，它们是位于 DDR 里的。</p>
<p>第 9 行的 blx 命令，并不是跳到 0x9006a4。这要根据当前的 PC 值来计算，在 dis 里写成 0x9006a4，这只是表示“如果程序从 0x900000 开始运行的话，第9 行就会跳到 0x9006a4”。现在程序被 boot ROM 复制到 0x80100000，从0x80100000 开始运行，我们需要根据机器码来计算出实际跳转的地址。  </p>
<p>blx 是相对跳转指令，要跳到“ pc + offset”这个地址去。程序从 0x8010000运行，运行到第 9 行时，如下计算新地址：  </p>
<figure class="highlight txt"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">PC     = 当前地址+8=0x8010004+8=0x801000C</span><br><span class="line">offset = 机器码 fa00016f 里的 bit[23:0]*4=0x16f*4=0x5BC</span><br><span class="line">new PC = PC + offset = 0x80105C8</span><br></pre></td></tr></table></figure>

<p>在 0x80105C8 这个位置，确实存有 copy_data 函数（这里没看懂，从哪知道之类有这个函数的？反正韦东山的裸机开发教程这么写的，我反正没理解，后面明白了再补充吧），所以：即使程序并不在链接地址 0x900000 上，它也可以运行。因为 blx 是相对跳转指令，它用的不是链接地址，它是“位置无关”的。使用“位置无关码”写出的代码，它可以在任何位置上运行，不一定要在“链接地址”上运行。</p>
<p>下面我们来分析一下实际板子上电后，程序是如何执行的  ：</p>
<p>（1）程序被 boot ROM 重定位到 0x80100000，并从这个地址开始执行第一条指令， 此时 pc &#x3D; 0x80100000 + 8 &#x3D; 0x80100008。</p>
<p>（2）执行到第 2 条指令“fa00016f”时，根据上述算法，它跳到地址0x80105C8 去执行 copy_data 函数。</p>
<p>（3）在执行完 copy_data 和 clean_bss 函数后，片内 RAM 0x900000 上已经有程序了。</p>
<p>（4）执行绝对跳转命令“ldr pc, &#x3D;main”，它是一条伪指令，真实指令是“ldr pc, [pc, #4] ; 900018 &lt;halt+0x8&gt;”。  </p>
<p>从 dis 文件里很容易看出，执行完这条指令后， pc 等于 dis 文件中“ 900018”上的值“009001d1”，所以程序跳到片内 RAM 去执行 main 函数了。  </p>
<p>注意：在 dis 文件中， main 函数的链接地址是 0x009001d1，往 pc 寄存器里赋值 0x009001d1 时， bit0 为 1，表示 main 函数的代码是用 Thumb 指令写的。 </p>
<h2 id="3-如何写位置无关码？"><a href="#3-如何写位置无关码？" class="headerlink" title="3. 如何写位置无关码？"></a><font size=3>3. 如何写位置无关码？</font></h2><p>那么我们应该如何写位置无关码呢？答：使用相对跳转命令 b 或 bl，并注意：</p>
<ul>
<li>重定位之前，不可使用绝对地址</li>
</ul>
<p>（a) 不可访问全局类变量（全局变量或 static 修饰的局部变量）。</p>
<p>（b) 不可访问有初始值的数组（初始值放在 rodata 里，需要绝对地址来访问）。</p>
<ul>
<li>重定位之后，使用 ldr pc &#x3D; xxx，跳转到绝对地址（ runtime address）  。</li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/ccdcfc3c.html">LV04-06-重定位-02-实现段的重定位</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2024年01月21日 - 16:04</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/ccdcfc3c.html" title="LV04-06-重定位-02-实现段的重定位">https://sumumm.github.io/post/ccdcfc3c.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/LV04-%E8%A3%B8%E6%9C%BA%E5%BC%80%E5%8F%91/" rel="tag"><i class="fa fa-tag"></i> LV04-裸机开发</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/71e36359.html" rel="prev" title="LV04-07-中断与异常-01-异常基础知识">
                  <i class="fa fa-angle-left"></i> LV04-07-中断与异常-01-异常基础知识
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/d5a3852e.html" rel="next" title="LV04-06-重定位-01-段与链接脚本基础">
                  LV04-06-重定位-01-段与链接脚本基础 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
